<!-- 实际呈现效果的组件 -->
<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>
<script>
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      myChart: null,
      allData: null,
      currentPage: 2,
      totalPage: 0,
      pageSize: 5,
      timerId: null,
      mapData: {},

      // 本地部署地址
      localAddress: "",
    };
  },

  created() {
    this.$socket.registerCallBack("mapData", this.getData);
  },
  mounted() {
    this.localAddress = window.location.origin;
    // window.vc = this;
    this.initChart();
    this.getData();

    // this.$socket.send({
    //   action: "getData",
    //   socketType: "mapData",
    //   chartName: "map",
    //   value: "",
    // });

    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    this.$socket.unregisterCallBack("mapType");
    window.removeEventListener("resize", this.screenAdapter);
  },

  methods: {
    //初始化echarts实例对象,要在dom结果加载完成后调用
    async initChart() {
      this.myChart = this.$echarts.init(this.$refs.map_ref, "chalk");

      //获取中国地图矢量数据
      // 地图存放在public/static/map下,是前端的静态文件,不能用this.$http,因为已经配置过baseURL了
      // 所以在前面重新导入axios实例
      const ret = await axios.get(this.localAddress + "/static/map/china.json");

      // 注册地图数据
      this.$echarts.registerMap("china", ret.data);

      const initOption = {
        title: {
          text: "重点供应商分布情况",
          left: 20,
          top: 20,
        },
        // 进行地图初始化设置
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            areaColor: "#2e72bf",
            borderColor: "#333",
          },
        },
        legend: {
          show: true,
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
      };
      this.myChart.setOption(initOption);

      // 监听地图点击事件
      // 利用导入的工具函数把汉字转换为拼音并查出路径
      this.myChart.on("click", async (arg) => {
        // 可以取到点击的省份
        // console.log(arg);
        const provinceInfo = getProvinceMapInfo(arg.name);
        // console.log(provinceInfo);

        // 获取省份地图的数据

        if (!this.mapData[provinceInfo.key]) {
          const { data: proData } = await axios.get(
            this.localAddress + provinceInfo.path
          );
          // console.log(proData);

          //缓存省份数据
          this.mapData[provinceInfo.key] = proData;
          this.$echarts.registerMap(provinceInfo.key, proData);
        }

        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        this.myChart.setOption(changeOption);
      });
    },
    // 获取数据
    async getData() {
      const { data: ret } = await this.$http.get("map");
      // console.log(retData);

      // 对allData进行赋值
      this.allData = ret;
      this.updateChart();
    },

    // 更新图表
    updateChart() {
      // 处理图表需要的数据,散点图
      const seriesArr = this.allData.map((item) => {
        // return的对象代表一个类别下的所有三点数据
        // 如果想在地图中显示散点数据,需要在系列中配置coordinateSystem:geo
        return {
          type: "effectScatter",
          name: item.name,
          data: item.children,
          coordinateSystem: "geo",
          rippleEffect: {
            scale: 5,
            brushType: "stroke",
          },
        };
      });

      const dataOption = {
        series: seriesArr,
      };

      this.myChart.setOption(dataOption);
    },

    screenAdapter() {
      // console.log(this.$refs.seller_ref.offsetWidth);
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },

        legend: {
          itemWidth: (titleFontSize / 2) * 1.5,
          itemHeight: (titleFontSize / 2) * 1.5,
          itemGap: (titleFontSize / 3) * 1.5,
          textStyle: {
            fontSize: (titleFontSize / 2) * 1.5,
          },
        },
      };

      this.myChart.setOption(adapterOption);

      // 收到调用resize才能产生效果
      this.myChart.resize();
    },

    //省份地图回到中国地图
    revertMap() {
      // console.log("双击了");
      const revertOption = {
        geo: {
          map: "china",
        },
      };

      this.myChart.setOption(revertOption);
    },
  },
};
</script>
<style lang=""></style>
